package com.agileboot.attendance.controller;

import com.agileboot.attendance.domain.order.OrderPersonnelApplicationService;
import com.agileboot.attendance.domain.order.command.AddOrderPersonnelCommand;
import com.agileboot.attendance.domain.order.command.BatchAddOrderPersonnelCommand;
import com.agileboot.attendance.domain.order.dto.OrderPersonnelDTO;
import com.agileboot.attendance.domain.order.dto.OrderPersonnelQuery;
import com.agileboot.attendance.domain.personnel.dto.PersonnelDTO;
import com.agileboot.common.core.base.BaseController;
import com.agileboot.common.core.dto.ResponseDTO;
import com.agileboot.common.core.page.PageDTO;
import com.agileboot.common.core.page.PageQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 订单人员关联控制器
 *
 * @author agileboot
 */
@Api(tags = "订单人员关联接口")
@RestController
@RequestMapping("/attendance/order/personnel")
@RequiredArgsConstructor
public class OrderPersonnelController extends BaseController {

    private final OrderPersonnelApplicationService orderPersonnelApplicationService;

    /**
     * 获取订单关联人员列表
     */
    @ApiOperation("获取订单关联人员列表")
    @PreAuthorize("@permission.has('attendance:order:query')")
    @GetMapping("/list/{orderId}")
    public ResponseDTO<List<OrderPersonnelDTO>> list(
            @ApiParam(value = "订单ID", required = true) @PathVariable Long orderId) {
        List<OrderPersonnelDTO> list = orderPersonnelApplicationService.getOrderPersonnelList(orderId);
        return ResponseDTO.ok(list);
    }
    
    /**
     * 分页查询订单关联人员列表
     */
    @ApiOperation("分页查询订单关联人员列表")
    @PreAuthorize("@permission.has('attendance:order:query')")
    @GetMapping("/page")
    public ResponseDTO<PageDTO<OrderPersonnelDTO>> page(OrderPersonnelQuery query) {
        PageDTO<OrderPersonnelDTO> pageDTO = orderPersonnelApplicationService.getOrderPersonnelPage(query);
        return ResponseDTO.ok(pageDTO);
    }
    
    /**
     * 获取可用人员列表，根据订单的供应商进行过滤
     */
    @ApiOperation("获取可用人员列表")
    @PreAuthorize("@permission.has('attendance:order:query')")
    @GetMapping("/available/{orderId}")
    public ResponseDTO<PageDTO<PersonnelDTO>> getAvailablePersonnel(
            @ApiParam(value = "订单ID", required = true) @PathVariable Long orderId,
            PageQuery pageQuery) {
        PageDTO<PersonnelDTO> pageDTO = orderPersonnelApplicationService.getAvailablePersonnelList(orderId, pageQuery);
        return ResponseDTO.ok(pageDTO);
    }
    
    /**
     * 添加订单人员关联
     */
    @ApiOperation("添加订单人员关联")
    @PreAuthorize("@permission.has('attendance:order:edit')")
    @PostMapping
    public ResponseDTO<Void> add(@Validated @RequestBody AddOrderPersonnelCommand command) {
        orderPersonnelApplicationService.addOrderPersonnel(command);
        return ResponseDTO.ok();
    }
    
    /**
     * 批量添加订单人员关联
     */
    @ApiOperation("批量添加订单人员关联")
    @PreAuthorize("@permission.has('attendance:order:edit')")
    @PostMapping("/batch")
    public ResponseDTO<Void> batchAdd(@Validated @RequestBody BatchAddOrderPersonnelCommand command) {
        orderPersonnelApplicationService.batchAddOrderPersonnel(command);
        return ResponseDTO.ok();
    }
    
    /**
     * 解除订单人员关联
     */
    @ApiOperation("解除订单人员关联")
    @PreAuthorize("@permission.has('attendance:order:edit')")
    @DeleteMapping("/{id}")
    public ResponseDTO<Void> remove(@PathVariable Long id) {
        orderPersonnelApplicationService.removeOrderPersonnel(id);
        return ResponseDTO.ok();
    }
} 